package com.bjpowernode.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Test06
 *
 * @author Cat老师，关注我，抖音搜索：java512
 */
public class Test06 {

    public static void main(String[] args) {

        //基于Executor框架实现线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                8,
                16,
                15,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(15),
                Executors.defaultThreadFactory(),
                //Executors.privilegedThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

        for (int i = 0; i < 40000; i++) {
            threadPoolExecutor.execute(new MyRunnable(i));
        }

        threadPoolExecutor.shutdown();

        //一些第三方自定义的拒绝策略
        org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport a;
        org.elasticsearch.common.util.concurrent.EsAbortPolicy b;
        io.netty.util.concurrent.RejectedExecutionHandlers c;
    }

    static class MyRunnable implements Runnable {
        private int i;

        public MyRunnable(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + ": " + this.i);
        }
    }
}